/***************************************************************************//**
 * @file Simulation.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef SIMULATION_CPP
#define SIMULATION_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <Simulation.h>

////////////////////////////////////////////////////////////////////////////////

Simulation::Simulation( QObject *parent ) :
    QObject( parent ),

    m_timerId ( 0 ),

    m_fdm ( 0 ),
    m_hid ( 0 ),
    m_win ( 0 )
{
    m_fdm = new FDM_Thread();
    m_hid = new HID_Thread();

    m_win = new MainWindow();

    memset( &m_fdmDataInp, 0, sizeof(FDM_DataInp) );
    memset( &m_fdmDataOut, 0, sizeof(FDM_DataOut) );

    memset( &m_guiDataInp, 0, sizeof(GUI_DataInp) );
    memset( &m_guiDataOut, 0, sizeof(GUI_DataOut) );

    m_timerId = QObject::startTimer( 40 );  // 25 Hz
}

////////////////////////////////////////////////////////////////////////////////

Simulation::~Simulation()
{
    if ( m_timerId ) QObject::killTimer( m_timerId );

    if ( m_hid )
    {
        while ( m_hid->isRunning() )
        {
            m_hid->quit();
        }

        delete m_hid;
        m_hid = 0;
    }

    if ( m_fdm )
    {
        while ( m_fdm->isRunning() )
        {
            m_fdm->quit();
        }

        delete m_fdm;
        m_fdm = 0;
    }

    if ( m_win ) delete m_win; m_win = 0;
}

////////////////////////////////////////////////////////////////////////////////

void Simulation::init()
{
    m_win->show();

    m_fdm->init();
    m_hid->init( m_win->effectiveWinId() );
}

////////////////////////////////////////////////////////////////////////////////

void Simulation::timerEvent( QTimerEvent *event )
{
    /////////////////////////////
    QObject::timerEvent( event );
    /////////////////////////////

    dataToFDM();
    dataToGUI();

    m_win->update( 0.0, m_guiDataInp, m_guiDataOut );
}

////////////////////////////////////////////////////////////////////////////////

void Simulation::dataToFDM()
{

}

////////////////////////////////////////////////////////////////////////////////

void Simulation::dataToGUI()
{

}
